package cn.edu.zafu.bigdata.hdb;

abstract class Shape {
    public abstract double area();
    public abstract double perimeter();
    public final void display(){
        System.out.println("Shape Type:"+ getClass().getSimpleName());
        System.out.println("Area:"+area());
        System.out.println("Perimeter:"+perimeter());
    }
}
class Circle extends Shape{
    private double radius;
    public Circle(double radius){
        this.radius=radius;
    }
    @Override
    public double area() { return Math.PI * radius * radius; }
    @Override
    public double perimeter(){
        return 2 * Math.PI * radius;
    }
}
class Rectangle extends Shape{
    private double length; // 长度
    private double width;  // 宽度

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double area() {
        return length * width; // 矩形的面积
    }

    @Override
    public double perimeter() {
        return 2 * (length + width); // 矩形的周长
    }
}
class TestShape {
    public static void main(String[] args) {
        // 创建 Circle 和 Rectangle 的实例
        Shape circle = new Circle(5);
        Shape rectangle = new Rectangle(4, 6);

        // 调用每个实例的 display() 方法
        circle.display();
        System.out.println();
        rectangle.display();

        // 验证抽象类的工作原理以及强制子类实现的行为
        // 无法直接实例化 Shape 类
        // Shape shape = new Shape(); // 这一行会导致编译错误
    }
}

final class FinalClass {
    // final 类不能被继承
}

// Uncommenting the following class will cause a compilation error
// class Subclass extends FinalClass { }

// final 方法示例
class BaseClass {
    public final void finalMethod() {
        System.out.println("This is a final method.");
    }
}

class Subclass extends BaseClass {
    // Uncommenting the following method will cause a compilation error
    // @Override
    // public void finalMethod() {
    //     System.out.println("Attempt to override final method.");
    // }
}

public class FinalKeywordDemo {
    public static void main(String[] args) {
        // 1. 测试 final 类
        // FinalClass fc = new FinalClass(); // 创建实例

        // 2. 测试 final 方法
        BaseClass base = new BaseClass();
        base.finalMethod();

        // 3. 测试 final 变量
        final int constantValue = 10;
        // Uncommenting the following line will cause a compilation error
        // constantValue = 20; // final 变量不能被修改
        System.out.println("Final variable: " + constantValue);
    }
}